<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>set manual page - Built-In Commands</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl Commands</a> <small>&gt;</small> set</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<H3><A NAME="M2">NAME</A></H3>
set &mdash; Read and write variables
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>set </B><I>varName </I>?<I>value</I>?<BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
Returns the value of variable <I>varName</I>.
If <I>value</I> is specified, then set
the value of <I>varName</I> to <I>value</I>, creating a new variable
if one does not already exist, and return its value.
If <I>varName</I> contains an open parenthesis and ends with a
close parenthesis, then it refers to an array element:  the characters
before the first open parenthesis are the name of the array,
and the characters between the parentheses are the index within the array.
Otherwise <I>varName</I> refers to a scalar variable.
<P>
If <I>varName</I> includes namespace qualifiers
(in the array name if it refers to an array element), or if <I>varName</I>
is unqualified (does not include the names of any containing namespaces)
but no procedure is active,
<I>varName</I> refers to a namespace variable
resolved according to the rules described under <B>NAME RESOLUTION</B> in
the <B><A HREF="../TclCmd/namespace.htm">namespace</A></B> manual page.
<P>
If a procedure is active and <I>varName</I> is unqualified, then
<I>varName</I> refers to a parameter or local variable of the procedure,
unless <I>varName</I> was declared to resolve differently through one of the
<B><A HREF="../TclCmd/global.htm">global</A></B>, <B><A HREF="../TclCmd/variable.htm">variable</A></B> or <B><A HREF="../TclCmd/upvar.htm">upvar</A></B> commands.
<H3><A NAME="M5">EXAMPLES</A></H3>
Store a random number in the variable <I>r</I>:
<P>
<PRE><B>set</B> r [expr {rand()}]</PRE>
<P>
Store a short message in an array element:
<P>
<PRE><B>set</B> anAry(msg) &quot;Hello, World!&quot;</PRE>
<P>
Store a short message in an array element specified by a variable:
<P>
<PRE><B>set</B> elemName &quot;msg&quot;
<B>set</B> anAry($elemName) &quot;Hello, World!&quot;</PRE>
<P>
Copy a value into the variable <I>out</I> from a variable whose name is
stored in the <I>vbl</I> (note that it is often easier to use arrays in
practice instead of doing double-dereferencing):
<P>
<PRE><B>set</B> in0 &quot;small random&quot;
<B>set</B> in1 &quot;large random&quot;
<B>set</B> vbl in[expr {rand() &gt;= 0.5}]
<B>set</B> out [<B>set</B> $vbl]</PRE>
<H3><A NAME="M6">SEE ALSO</A></H3>
<B><A HREF="../TclCmd/expr.htm">expr</A></B>, <B><A HREF="../TclCmd/global.htm">global</A></B>, <B><A HREF="../TclCmd/namespace.htm">namespace</A></B>, <B><A HREF="../TclCmd/proc.htm">proc</A></B>, <B><A HREF="../TclCmd/trace.htm">trace</A></B>, <B><A HREF="../TclCmd/unset.htm">unset</A></B>, <B><A HREF="../TclCmd/upvar.htm">upvar</A></B>, <B><A HREF="../TclCmd/variable.htm">variable</A></B>
<H3><A NAME="M7">KEYWORDS</A></H3>
<A href="../Keywords/R.htm#read">read</A>, <A href="../Keywords/W.htm#write">write</A>, <A href="../Keywords/V.htm#variable">variable</A>
<div class="copy">Copyright &copy; 1993 The Regents of the University of California.
<BR>Copyright &copy; 1994-1996 Sun Microsystems, Inc.
</div>
</BODY></HTML>
